# coding=utf-8
import os, sys
parentdir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, parentdir)
from api.base_api.login_base_api import LoginBaseApi
from api.base_api.car_estimate import CarEstimate
from api.base_api.amount_calculate import interestPlan,rentPlan


class CarEstimateTest(LoginBaseApi):
    ''' 测试车辆评估租车 '''

    def setUp(self):
        self.car_estimate_url = "http://192.168.1.141:8001/car-estimate"
        self.register_user_mobile = '18245135693'
        self.unregistered_mobile = ''
        self.password = '123abc'

    def test_rent_plan_success(self):
        ''' 车辆评估低租金 '''
        header = LoginBaseApi().login_base_api(self.register_user_mobile, self.password)
        payload = CarEstimate().car_estimate(self.register_user_mobile, 'PURCHASE')
        response = self.post(self.car_estimate_url, payload, header)
        self.assertEqual(response.status_code, 200)
        #低租金最高估值
        car_good = float(response.json()['carGood']) * 10000
        car_bad = float(response.json()['carBad']) * 10000
        list = [car_good, car_bad]
        for car_amount in list:

            if car_amount == car_good:
                status = 'highestPrice'
            else:
                status = 'lowestPrice'

            for j in range(3):
                period = (j+1)*12
                car_type = rentPlan(car_amount, period)

                response_corpus = response.json()['rentPlan'][status][j]['corpus']
                corpus = car_type[0]
                self.assertEqual(response_corpus, corpus)

                response_interest = response.json()['rentPlan'][status][j]['interest']
                interest = car_type[1]
                self.assertEqual(response_interest, interest)

                response_eachPeriod = response.json()['rentPlan'][status][j]['eachPeriod']
                eachPeriod = car_type[2]
                self.assertEqual(response_eachPeriod, eachPeriod)

                response_allPeriod = response.json()['rentPlan'][status][j]['allPeriod']
                allPeriod = car_type[3]
                self.assertEqual(response_allPeriod, allPeriod)

                response_rest = response.json()['rentPlan'][status][j]['rest']
                rest = car_type[4]
                self.assertEqual(response_rest, rest)
                self.assertEqual(response.json()['rentPlan'][status][j]['period'], period)

    def test_interest_plan_success(self):
        ''' 车辆评估低利息 '''
        header = LoginBaseApi().login_base_api(self.register_user_mobile, self.password)
        payload = CarEstimate().car_estimate(self.register_user_mobile, 'PURCHASE')
        response = self.post(self.car_estimate_url, payload, header)
        self.assertEqual(response.status_code, 200)
        # 低租金最高估值
        car_good = float(response.json()['carGood']) * 10000
        car_bad = float(response.json()['carBad']) * 10000

        list = [car_good, car_bad]
        for car_amount in list:

            if car_amount == car_good:
                status = 'highestPrice'
            else:
                status = 'lowestPrice'

            for j in range(3):
                period = (j + 1) * 12
                car_type = interestPlan(car_amount, period)
                response_corpus = response.json()['interestPlan'][status][j]['corpus']
                corpus = car_type[0]
                self.assertEqual(response_corpus, corpus)

                response_interest = response.json()['interestPlan'][status][j]['interest']
                interest = car_type[1]
                self.assertEqual(response_interest, interest)

                response_eachPeriod = response.json()['interestPlan'][status][j]['eachPeriod']
                eachPeriod = car_type[2]
                self.assertEqual(response_eachPeriod, eachPeriod)

                response_allPeriod = response.json()['interestPlan'][status][j]['allPeriod']
                allPeriod = car_type[3]
                self.assertEqual(response_allPeriod, allPeriod)

                response_rest = response.json()['interestPlan'][status][j]['rest']
                rest = car_type[4]
                self.assertEqual(response_rest, rest)
                self.assertEqual(response.json()['interestPlan'][status][j]['period'], period)

    def test_unregistered_car_estimate(self):
        ''' 未登录用户评估 '''
        payload = CarEstimate().car_estimate(self.unregistered_mobile, 'PURCHASE')
        response = self.post(self.car_estimate_url, payload)
        message = response.json()['message']
        self.assertNotEqual(response.status_code, 200)
        self.assertEqual(message, '请求数据失败。')

